<?php
/*
 * 框架入口
 */

define('IN_RONCRM',true);
//系统时间
define('SYS_TIME', time());
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');

define('RF_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);
define('CONFIGS',RF_PATH.'configs'.DIRECTORY_SEPARATOR);

define('JS_PATH',RonBase::load_config('system','js_path'));
define('CSS_PATH',RonBase::load_config('system','css_path'));
define('IMG_PATH',RonBase::load_config('system','img_path'));
define('CACHE_PATH',RONCRM_PATH.'caches'.DIRECTORY_SEPARATOR);
header('Context-type : text/html; charset=utf-8');
class RonBase {
	
	public static function create_app(){
		return new CApplication();
	} 
	
	public static function autoload($classname){
		if(isset(self::$_coreClasses[$classname])){
			include(RF_PATH.self::$_coreClasses[$classname]);
		} elseif(substr($classname, -6) == '_Model') {
			include(RF_PATH.'models'.DIRECTORY_SEPARATOR.$classname.'.php');
		}
		return true;
	}
	
	/*
	 * 加载配置文件
	 */
	public static function load_config($file, $key = '', $default = '', $reload = false){
		static $configs = array();
		if(!$reload && isset($configs[$file]) ){
			if(empty($key)){
				return $configs[$file];
			} elseif(isset($configs[$file][$key])){
				return $configs[$file][$key];
			} else {
				return $default;
			}
		} 
	
		$path = CONFIGS.$file.'.php';
		if(file_exists($path)){
			$configs[$file] = include $path;
		}
		if(empty($key)){
			return $configs[$file];
		} elseif(isset($configs[$file][$key])){
			return $configs[$file][$key];
		} else {
			return $default;
		}		
	}
	
	private static $_coreClasses = array(
		'CApplication' => 'base/CApplication.php',
		'CParam' => 'base/CParam.php',
		'CAdmin' => 'base/CAdmin.php',
		'CAdmin_crmentity' => 'base/CAdmin_crmentity.php',
		'CProcess' => 'base/CProcess.php',
		'CTree' => 'base/CTree.php',
		'CSession_Mysql'=>'base/CSession_Mysql.php',
		'CSysfunction' => 'base/CSysfunction.php',	
		'CAttachment' => 'base/CAttachment.php',
		'CList' => 'base/CList.php',
	
		'Model' => 'db/Model.php',
		'DBFactory' => 'db/DBFactory.php',
		'Mysql' =>'db/Mysql.php',
	
		'Cache_Factory' => 'cache/Cache_Factory.php',
		'Cache_File' => 'cache/Cache_File.php',
		'Caches' => 'cache/Caches.php',
		
		'CValidator' => 'validators/CValidator.php',
		'SlashesValidator' => 'validators/SlashesValidator.php',
		'GlobalValidator' => 'validators/GlobalValidator.php',
		'CRequiredValidator'=>'validators/CRequiredValidator.php',
		'CEmailValidator'=>'validators/CEmailValidator.php',
	);
}

spl_autoload_register(array('RonBase','autoload'));